/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-03     MBW       the first version
 */
#include "uart_data.h"
static uint8_t _UartDataTemp[50];
static float uart_check_time_ms[3];
u8 data2uart[100];
_uart_of_st uart_data={0};
#define uart_BYTE0(dwTemp)  (*(char*)(&dwTemp))
#define uart_BYTE1(dwTemp)  (*((char*)(&dwTemp)+1))
#define uart_BYTE2(dwTemp)  (*((char*)(&dwTemp)+2))
#define uart_BYTE3(dwTemp)  (*((char*)(&dwTemp)+3))
void uart_GetOneByte(uint8_t data)
{
    static u8 _data_len = 0, _data_cnt = 0;
    static u8 rxstate = 0;

    if (rxstate == 0 && data == 0xAA)
    {
        rxstate = 1;
        _UartDataTemp[0] = data;
    }
    else if (rxstate == 1 && (data == 0x61 || data == 0xFF))  //0x61  0xFF
    {
        rxstate = 2;
        _UartDataTemp[1] = data;
    }
    else if (rxstate == 2)
    {
        rxstate = 3;
        _UartDataTemp[2] = data;
    }
    else if (rxstate == 3 && data < 250)
    {
        rxstate = 4;
        _UartDataTemp[3] = data;
        _data_len = data;
        _data_cnt = 0;
    }
    else if (rxstate == 4 && _data_len > 0)
    {
        _data_len--;
        _UartDataTemp[4 + _data_cnt++] = data;
        if (_data_len == 0)
            rxstate = 5;
    }
    else if (rxstate == 5)
    {
        rxstate = 6;
        _UartDataTemp[4 + _data_cnt++] = data;
    }
    else if (rxstate == 6)
    {
        rxstate = 0;
        _UartDataTemp[4 + _data_cnt] = data;
        //      DT_data_cnt = _data_cnt+5;
        //
        uart_DataAnl(_UartDataTemp, _data_cnt + 5); //
    }
    else
    {
        rxstate = 0;
    }
}
static void uart_DataAnl(uint8_t *data, uint8_t len)
{
    u8 check_sum1 = 0, check_sum2 = 0;
    if (*(data + 3) != (len - 6)) //ÅÐ¶ÏÊý¾Ý³¤¶ÈÊÇ·ñÕýÈ·
        return;
    for (u8 i = 0; i < len - 2; i++)
    {
        check_sum1 += *(data + i);
        check_sum2 += check_sum1;
    }
    if ((check_sum1 != *(data + len - 2)) || (check_sum2 != *(data + len - 1))) //ÅÐ¶ÏsumÐ£Ñé
        return;
    //================================================================================

    if (*(data + 2) == 0XE3) //K210
    {
        uart_data.box_left_x     = *((s16 *)(data + 4));
        uart_data.box_left_y     = *((s16 *)(data + 6));
        uart_data.box_length_x   = *((s16 *)(data + 8));
        uart_data.box_length_y   = *((s16 *)(data + 10));
        uart_data.confidence     = *((s16 *)(data + 12));
        uart_data.label          = *((s16 *)(data + 14));
        uart_data.frame_numbers  = *((s16 *)(data + 15));
    }
    else if (*(data + 2) == 0XE4) //FLOW
    {
        uart_data.vx          = *((s16 *)(data + 4));
        uart_data.vy          = *((s16 *)(data + 6));
        uart_data.dx          = *((s16 *)(data + 8));
        uart_data.dy          = *((s16 *)(data + 10));
        uart_data.flow_dis    = *((s16 *)(data + 12));
        uart_data.of_quality  = *((s16 *)(data + 16));
    }
}
void USART_report_K210Res(s16 lx,s16 ly,//0XE3 左上点的坐标
        s16 bx,s16 by,//矩形框的长宽
        s16 c,u8 n,u8 z)//置信率c，标号n，帧数z
{
  u8 _cnt=0;
    u8 sc = 0;
    u8 ac = 0;
    u8 i;
    data2uart[_cnt++] = 0xAA;
    data2uart[_cnt++] = 0xFF;
    data2uart[_cnt++] = 0xE3;
    data2uart[_cnt++] = 12;



    data2uart[_cnt++] = uart_BYTE0(lx);
    data2uart[_cnt++] = uart_BYTE1(lx);

    data2uart[_cnt++] = uart_BYTE0(ly);
    data2uart[_cnt++] = uart_BYTE1(ly);

    data2uart[_cnt++] = uart_BYTE0(bx);
    data2uart[_cnt++] = uart_BYTE1(bx);

    data2uart[_cnt++] = uart_BYTE0(by);
    data2uart[_cnt++] = uart_BYTE1(by);

    data2uart[_cnt++] = uart_BYTE0(c);
    data2uart[_cnt++] = uart_BYTE1(c);

    data2uart[_cnt++] = n;
    data2uart[_cnt++] = z;

    for( i=0;i<data2uart[3]+4; i++)
    {
        sc += data2uart[i];
        ac += sc;
    }

    data2uart[_cnt++] = sc;
    data2uart[_cnt++] = ac;

    for(i=0;i<_cnt;i++)USART_send_char(data2uart[i]);
}
void USART_report_FlowRes(s16 vx,s16 vy,s16 dx ,s16 dy ,s32 dis,u8 q )
{
  u8 _cnt=0;
    u8 sc = 0;
    u8 ac = 0;
    u8 i;
    data2uart[_cnt++] = 0xAA;
    data2uart[_cnt++] = 0xFF;
    data2uart[_cnt++] = 0xE4;
    data2uart[_cnt++] = 13;



    data2uart[_cnt++] = uart_BYTE0(vx);
    data2uart[_cnt++] = uart_BYTE1(vx);

    data2uart[_cnt++] = uart_BYTE0(vy);
    data2uart[_cnt++] = uart_BYTE1(vy);

    data2uart[_cnt++] = uart_BYTE0(dx);
    data2uart[_cnt++] = uart_BYTE1(dx);

    data2uart[_cnt++] = uart_BYTE0(dy);
    data2uart[_cnt++] = uart_BYTE1(dy);

    data2uart[_cnt++] = uart_BYTE0(dis);
    data2uart[_cnt++] = uart_BYTE1(dis);
    data2uart[_cnt++] = uart_BYTE2(dis);
    data2uart[_cnt++] = uart_BYTE3(dis);

    data2uart[_cnt++] = q;

    for( i=0;i<data2uart[3]+4; i++)
    {
        sc += data2uart[i];
        ac += sc;
    }

    data2uart[_cnt++] = sc;
    data2uart[_cnt++] = ac;

    for(i=0;i<_cnt;i++)USART_send_char(data2uart[i]);
}
void USART_send_char(u8 c)
{
    while((USART1->SR&0X40)==0);
    USART1->DR=c;
}
